package com.tuo51.wx.mp.controller;

import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.common.bean.menu.WxMenuButton;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.MalformedURLException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;


@Slf4j
@RestController
@RequestMapping("/mp/menu/{appid}/{appsecret}")
public class WxMpMenuController {
    private WxMpService wxService;

    @Autowired
    public WxMpMenuController(WxMpService wxService) {
        this.wxService = wxService;
    }

    @GetMapping("/create")
    public String menuCreateSample(HttpServletRequest req, HttpServletResponse resp,
                                   @PathVariable String appid, @PathVariable String appsecret) throws WxErrorException, MalformedURLException {

        String serverName = req.getServerName();
        String host = req.getScheme() + "://" + serverName;

        Map<String, String> map = new HashMap<String, String>();
        Enumeration headerNames = req.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String key = (String) headerNames.nextElement();
            String value = req.getHeader(key);
            map.put(key, value);
        }

        log.info("menu base header : " + map);

        log.info("menu base url : " + host);
//        String host = "http://emp.dev.tuo51.com";

        WxMenu menu = new WxMenu();
        WxMenuButton button1 = new WxMenuButton();
        button1.setName("测评");
        WxMenuButton button2 = new WxMenuButton();
        button2.setName("育儿宝箱");
        WxMenuButton button3 = new WxMenuButton();
        button3.setName("宝妈课堂");

        menu.getButtons().add(button1);
        menu.getButtons().add(button2);
        menu.getButtons().add(button3);

        WxMenuButton button11 = new WxMenuButton();
        button11.setName("测评报告");
        button11.setType(WxConsts.MenuButtonType.VIEW);
        button11.setUrl(host + "/redirect/assessReports");

        WxMenuButton button12 = new WxMenuButton();
        button12.setName("父母自测");
        button12.setType(WxConsts.MenuButtonType.VIEW);
        button12.setUrl(host + "/redirect/assessIndex");

        WxMenuButton button13 = new WxMenuButton();
        button13.setName("预约测评");
        button13.setType(WxConsts.MenuButtonType.MINIPROGRAM);
        button13.setAppId("wx69331d022bce085c");
        button13.setUrl("http://mp.weixin.qq.com");
        button13.setPagePath("pages/index");

        button1.getSubButtons().add(button11);
        button1.getSubButtons().add(button12);
        button1.getSubButtons().add(button13);

//        id
//        1- 大运动 2- 精细动作 3- 适应能力 4- 社会性 5- 语言
//
//        路径  /fineMotor?attribute= {id }
        WxMenuButton button21 = new WxMenuButton();
        button21.setName("大运动");
        button21.setType(WxConsts.MenuButtonType.VIEW);
        button21.setUrl(host + "/fineMotor?attribute=1");

        WxMenuButton button22 = new WxMenuButton();
        button22.setName("精细动作");
        button22.setType(WxConsts.MenuButtonType.VIEW);
        button22.setUrl(host + "/fineMotor?attribute=2");

        WxMenuButton button23 = new WxMenuButton();
        button23.setName("适应能力");
        button23.setType(WxConsts.MenuButtonType.VIEW);
        button23.setUrl(host + "/fineMotor?attribute=3");

        WxMenuButton button24 = new WxMenuButton();
        button24.setName("社会性");
        button24.setType(WxConsts.MenuButtonType.VIEW);
        button24.setUrl(host + "/fineMotor?attribute=4");

        WxMenuButton button25 = new WxMenuButton();
        button25.setName("语言");
        button25.setType(WxConsts.MenuButtonType.VIEW);
        button25.setUrl(host + "/fineMotor?attribute=5");

        button2.getSubButtons().add(button21);
        button2.getSubButtons().add(button22);
        button2.getSubButtons().add(button23);
        button2.getSubButtons().add(button24);
        button2.getSubButtons().add(button25);

        WxMenuButton button31 = new WxMenuButton();
        button31.setName("知识宝库");
        button31.setType(WxConsts.MenuButtonType.VIEW);
        button31.setUrl(host + "/redirect/articleList");

        WxMenuButton button32 = new WxMenuButton();
        button32.setName("个人中心");
        button32.setType(WxConsts.MenuButtonType.VIEW);
        button32.setUrl(host + "/");


        button3.getSubButtons().add(button31);
        button3.getSubButtons().add(button32);

        return wxService.switchoverTo(appid).getMenuService().menuCreate(menu);
    }
}
